package br.com.android.pubclub.web;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import br.com.android.pubclub.R;
import br.com.android.pubclub.basicas.Saldo;

public class PresentearAdapter extends BaseAdapter {

	ArrayList<Saldo> meuSaldo;

	public PresentearAdapter(ArrayList<Saldo> meuSaldo) {

		this.meuSaldo = meuSaldo;

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return meuSaldo.size();
	}

	@Override
	public Saldo getItem(int position) {
		// TODO Auto-generated method stub

		return meuSaldo.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final Holder holder;
		Context ctx = parent.getContext();

		final Saldo saldo = meuSaldo.get(position);

		if (convertView == null) {

			convertView = LayoutInflater.from(ctx).inflate(
					R.layout.linha_presentear, null);

			holder = new Holder();
			holder.txtBebidaDesc = (TextView) convertView
					.findViewById(R.id.txtBedidaDesc);
			holder.txtMeuSaldo = (TextView) convertView
					.findViewById(R.id.txtSaldo);
			holder.imgViewAumentar = (ImageView) convertView
					.findViewById(R.id.imgViewAumentar);
			holder.imgViewDiminuir = (ImageView) convertView
					.findViewById(R.id.imgViewDiminuir);
			holder.edtQtdDoses = (EditText) convertView
					.findViewById(R.id.edtQtdDoses);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		holder.txtBebidaDesc.setText(saldo.getBedida().getDescricao());
		String medida = saldo.getTotal() > 1 ? saldo.getBedida().getMedida()
				.getDescricao()
				+ "s" : saldo.getBedida().getMedida().getDescricao();
		holder.txtMeuSaldo.setText(String.valueOf(saldo.getTotal()) + " "
				+ medida);
		holder.imgViewAumentar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				if (holder.edtQtdDoses.getText().toString() == null || holder.edtQtdDoses.getText().toString().equals("")) {

					holder.edtQtdDoses.setText("1");
					holder.imgViewDiminuir.setEnabled(true);
				} else {
					int qtd = Integer.valueOf(holder.edtQtdDoses.getText()
							.toString());
					qtd += 1;
					

					if (qtd > saldo.getTotal()){
						holder.imgViewAumentar.setEnabled(false);
					}else{
						holder.edtQtdDoses.setText(String.valueOf(qtd));	
					}

				}

			}
		});
		holder.imgViewDiminuir.setEnabled(false);
		holder.imgViewDiminuir.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (holder.imgViewDiminuir.isEnabled()) {

					int qtd = Integer.valueOf(holder.edtQtdDoses.getText()
							.toString());
					if (qtd == saldo.getTotal())
						holder.imgViewAumentar.setEnabled(true);

					qtd -= 1;

					if (qtd == 0) {
						holder.edtQtdDoses.setText(null);
						holder.imgViewDiminuir.setEnabled(false);
					} else {

						holder.edtQtdDoses.setText(String.valueOf(qtd));
					}
				}
			}
		});

		return convertView;
	}

	static class Holder {

		TextView txtBebidaDesc;
		TextView txtMeuSaldo;
		ImageView imgViewAumentar;
		ImageView imgViewDiminuir;
		EditText edtQtdDoses;

	}

}
